package com.dpkit.adapter.orm.impl.mybatis;

import com.dpkit.adapter.DatabaseDialect;
import com.dpkit.adapter.OrmAdapter;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

public class MyBatisAdapter implements OrmAdapter<Map<String, Object>> {
    private final SqlSessionFactory sqlSessionFactory;
    private final DatabaseDialect dialect;

    @Override
    public void save(String entityName, Map<String, Object> data) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            String statement = entityName + ".insert";
            session.insert(statement, data);
            session.commit();
        }
    }

    @Override
    public void batchSave(String entityName, List<Map<String, Object>> batchData) {

    }

    @Override
    public Class<Map<String, Object>> generateEntityClass(EntityDefinition definition) {
        // MyBatis不需要生成实体类，但需要生成XML映射文件
        generateMyBatisXml(definition);
        return Map.class;
    }

    @Override
    public DataSource getDataSource() {
        return null;
    }

    private void generateMyBatisXml(EntityDefinition definition) {
        String xmlContent = buildMapperXml(definition);
        // 将XML写入配置的mapper位置
        writeMapperFile(definition.getEntityName(), xmlContent);
    }
    
    // 其他实现方法...
}